package com.csyf.manager.service.impl;

import com.csyf.manager.dao.SysUserMapper;
import com.csyf.manager.dao.SysUserRoleMapper;
import com.csyf.manager.entity.SysUser;
import com.csyf.manager.entity.SysUserRole;
import com.csyf.manager.service.SysUserService;
import com.csyf.manager.utils.ResultEntity;
import com.csyf.manager.utils.ResultUtli;
import com.csyf.manager.utils.page.JqGridVo;
import com.csyf.manager.utils.page.PageUtils;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: casper
 * @Description: 用户ServiceImpl
 * @Date: Created in 2018/11/19 11:55
 */
@Slf4j
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 根据用户名查询用户
     * @param username
     * @return user
     */
    @Override
    public SysUser selectByUsername(String username) {
        return sysUserMapper.selectByUsername(username);
    }

    /**
     * 更新用户最近登录时间
     * @param user
     */
    @Override
    public void updateLastLoginTime(SysUser user) {
        sysUserMapper.updateLastLoginTime(user);
    }

    /**
     * 用户列表
     * @param vo
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public JqGridVo queryUserList(SysUser vo, Integer pageIndex, Integer pageSize) {

        PageHelper.startPage(pageIndex, pageSize);
        PageHelper.orderBy("create_time DESC");

        Example example = new Example(SysUser.class);
        Example.Criteria criteria = example.createCriteria();
        PageUtils.andEqualTo(criteria, "userName", vo.getUserName());
        PageUtils.andEqualTo(criteria, "status", "1");

        List<SysUser> select =  sysUserMapper.selectByExample(example);
        return PageUtils.pasrJqGrid(select);
    }

    /**
     * 新增用户
     * @param userForm
     * @return
     */
    @Override
    public int addUser(SysUser userForm) {
        return sysUserMapper.insert(userForm);
    }

    /**
     * 新增用户并获取用户Id
     * @param userForm
     * @return
     */
    @Override
    public void addUserAndGetId(SysUser userForm) {
        sysUserMapper.insertAndGetId(userForm);
    }


    /**
     * 根据用户id查询用户
     * @param userId
     * @return
     */
    @Override
    public SysUser selectByUserId(String userId) {
        return sysUserMapper.selectByUserId(userId);
    }

    /**
     * 根据用户id更新用户信息
     * @param userForm
     * @return
     */
    @Override
    public int updateByUserId(SysUser userForm) {
        return sysUserMapper.updateByUserId(userForm);
    }

    /**
     * 根据用户id修改用户密码
     * @param user
     */
    @Override
    public void updatePwdByUserId(SysUser user) {
        sysUserMapper.updatePwdByUserId(user);
    }

    /**
     * 根据用户id集合批量更新用户状态
     * @param userIds
     * @param status
     * @return
     */
    @Override
    public int updateStatusBatch(List<String> userIds, Integer status) {
        Map<String,Object> params = new HashMap<>(2);
        params.put("userIds", userIds);
        params.put("status", status);
        return sysUserMapper.updateStatusBatch(params);
    }

    /**
     * 根据用户id分配角色集合
     * @param userId
     * @param roleIdsList
    * @return
     */
    @Override
    public void addAssignRole(Long userId, List<String> roleIdsList) {

        try {
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(userId);
            sysUserRoleMapper.delete(userRole);
            for (String roleId : roleIdsList) {
                userRole.setRoleId(Long.valueOf(roleId));
                sysUserRoleMapper.insert(userRole);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
